# coding=utf-8
__author__ = 'Vlad'

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import sys, time, random

sys.path.append('C:\\')
driver = webdriver.Firefox()
workspace_dir = 'D:\\'
driver.implicitly_wait(5)

#Функция для клика по кнопке при известном XPATH
#0 - XPATH-путь к кнопке
#1 - Сообщение, которое выводится в консоль при не обнаружение данной кнопки
#2 - Часть имени скриншота, который снимается при этом
def btn_xpath_click(btn_xpath, message1='', message2=''):
    try:
        driver.find_element_by_xpath(btn_xpath).click()
        time.sleep(0.1)
    except NoSuchElementException, e:
        print message1
        driver.get_screenshot_as_file(workspace_dir + str(time.strftime("%Y%m%d-%H%M%S", time.localtime())) + '-' + message2 + '.png')
        driver.quit()
        print driver.title

#Open url
test_url = 'http://localhost/php4dvd/'
driver.get(test_url)

#Authorization
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("admin")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("admin")
driver.find_element_by_name("submit").click()
if 'Log out' not in driver.find_element_by_xpath("//*[@id='wrapper']/header/div/nav/ul/li[4]/a").text:
    print 'We have some problem with authorization'
    driver.quit()
    print driver.title
else:
    print 'Successfully logged in'

#checking general quatity of dvds already on the page
quantity=len(driver.find_elements_by_xpath("//*[@id='results']/a"))

#add dvd
btn_xpath_click("//*[@id='content']/section/nav/ul/li[1]/div/div/a/img")

name='test dvd ' + str(quantity+1)
year=random.randint(1900,2013)

#filling the form
driver.find_element_by_name("name").clear()
driver.find_element_by_name("name").send_keys(name)
driver.find_element_by_name("aka").click()
driver.find_element_by_name("aka").clear()
driver.find_element_by_name("aka").send_keys("also known as " + str(quantity+1))
driver.find_element_by_name("year").clear()
driver.find_element_by_name("year").send_keys(year)
driver.find_element_by_name("duration").clear()
driver.find_element_by_name("duration").send_keys(str(quantity+1))
driver.find_element_by_name("rating").clear()
driver.find_element_by_name("rating").click()
driver.find_element_by_name("rating").send_keys("unknown")
driver.find_element_by_id("cover").send_keys("D:\\flyer.jpg")

#checking rating field for valid
if driver.find_element_by_name("rating").get_attribute('class') != 'number error':
    print 'trouble with rating validness'
    driver.quit()
    print driver.title
else:
    driver.find_element_by_name("rating").clear()
    driver.find_element_by_name("rating").send_keys("22")
    driver.find_element_by_id("submit").click()

#checking title after creation
if driver.find_element_by_xpath("//*[@id='movie']/div[2]/h2").text !=name + ' ' + '(' + str(year) + ')':
    print 'something wrong with DVD page title'
    driver.quit()
    print driver.title

btn_xpath_click("//*[@id='wrapper']/header/div/nav/ul/li/a")

if len(driver.find_elements_by_xpath("//*[@id='results']/a")) != quantity+1:
    print 'new dvd wasnt added'
    driver.quit()
    print driver.title
else:
    print 'movie was successfully added'

time.sleep(1)

#open dvd for removing
btn_xpath_click("//*[@id='results']/a")

#click remove button
btn_xpath_click("//*[@id='content']/section/nav/ul/li[5]/div/div/a")

#accepting alert
alert = driver.switch_to_alert()
alert.accept()
time.sleep(1)

#checking if it was removed
if len(driver.find_elements_by_xpath("//*[@id='results']/a")) != quantity:
    print 'seems like dvd wasn\'t removed'
    driver.quit()
    print driver.title
else:
    print 'dvd was removed'

time.sleep(1)

driver.quit()